package gui.admin;

import gui.AbstractDefensiveDriverPanel;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

public class ProgressPanelImpl extends AbstractAdminPanelImpl implements ProgressPanel {

	private ProgressBar progressBar;
	private Label message;
	private AbstractDefensiveDriverPanel caller;
	
	public ProgressPanelImpl(Display display, Shell shell) {
		super(display, shell);
		progressBar = new ProgressBar(shell, SWT.SMOOTH);
		message = new Label(shell, SWT.CENTER);
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	public void open() {
		if(caller == null) {
			throw new NullPointerException();
		}
		
		if(isHidden()) {
			hide(false);
		}
		
		caller.setEnabled(false);

		shell.setSize(250, 115);
		shell.setText("");
		menuBar.dispose();

		progressBar.setBounds(20, 10, 204, 24);
		
		message.setBounds(20, 40, 204, 48);
		message.setFont(new Font(display, "Arial", 16, SWT.NONE));
		
		shell.open();
		
		for(int i = 0; i <=progressBar.getMaximum(); i++) {
			try {
				Thread.sleep(50);
			} catch (InterruptedException ie) {}
			
			progressBar.setSelection (i);
		}
		close();
		caller.setEnabled(true);
	}

	public void setMessage(String message) {
		this.message.setText(message);
		this.message.update();
	}

	public void close() {
		shell.dispose();
	}

	public void setCaller(AbstractDefensiveDriverPanel caller) {
		this.caller = caller;
	}

	public boolean isDisposed() {
		return shell.isDisposed();
	}

}
